package com.shuoyue.ycgk.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.appdepends.base.BaseFragment;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;
import com.shuoyue.ycgk.net.exception.NetErrorException;
//import com.shuoyue.ycgk.ui.account.AccountLoginActivity;
import com.shuoyue.ycgk.ui.account.login.LoginActivity;
import com.shuoyue.ycgk.utils.XToast;
import com.shuoyue.ycgk.views.dialog.SpotsDialog;
import com.uber.autodispose.AutoDisposeConverter;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

import static android.app.Activity.RESULT_OK;
//import static com.shuoyue.ycgk.ui.account.AccountLoginActivity.REQUEST_CODE;

/**
 * @author azheng
 * @date 2018/4/24.
 * GitHub：https://github.com/RookieExaminer
 * Email：wei.azheng@foxmail.com
 * Description：
 */
public abstract class BaseMvpFragment<T extends BasePresenter> extends BaseFragment implements BaseView {

    protected T mPresenter;
    private CompositeDisposable mCompositeDisposable;
    SpotsDialog spotsDialog;

    public String getTitle() {
        return "";
    }


    @Override
    protected void initData() {
        super.initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mCompositeDisposable = new CompositeDisposable();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear(); // clear时网络请求会随即cancel
            mCompositeDisposable = null;
        }
        super.onDestroyView();
    }

    @Override
    public void showLoading(String title) {
        if (spotsDialog == null) {
            spotsDialog = new SpotsDialog(mContext, title);
        }
        if (!TextUtils.isEmpty(title)) {
            spotsDialog.setText(title);
        } else {
            spotsDialog.setText("");
        }
        if (!spotsDialog.isShowing()) {
            spotsDialog.show();
        }
    }

    @Override
    public void showLoading() {
        showLoading(null);
    }

    @Override
    public void hideLoading() {
        if (spotsDialog != null && spotsDialog.isShowing()) {
            spotsDialog.dismiss();
        }
        if (smartRefreshLayout != null) {
            smartRefreshLayout.finishRefresh();
            smartRefreshLayout.finishLoadMore();
        }
    }

    SmartRefreshLayout smartRefreshLayout;

    public void registFinishLoad(SmartRefreshLayout smartRefreshLayout) {
        this.smartRefreshLayout = smartRefreshLayout;
//        smartRefreshLayout.setEnableLoadMore(false);
    }

    @Override
    public void onError(NetErrorException throwable) {

    }

    boolean isJumpToLogin = false;//防止多个请求重复

    @Override
    public void needLogin() {

        if (mActivity instanceof BaseMvpActivity) {
            ((BaseMvpActivity) mActivity).needLogin();
        } else if (!isJumpToLogin) {
            SPUtils.clearUserInfo(mContext);
            MemeryCatch.token = "";
//            startActivityForResult(new Intent(mActivity, LoginActivity.class).putExtra("requestCode",
//                    LoginActivity.REQUEST_CODE),
//                    LoginActivity.REQUEST_CODE);
            LoginActivity.start(mActivity);
            isJumpToLogin = true;
        }

    }

    @Override
    public void clearUserInfo() {
        MemeryCatch.token = "";
    }

    @Override
    public <T> AutoDisposeConverter<T> bindAutoDispose() {
        return null;
    }

    @Override
    public void bindDispos(Disposable disposable) {
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.add(disposable);
        }
    }

    public void registEmptyView(AppBaseQuickAdapter mAdapter) {
        View layEmpty = LayoutInflater.from(mContext).inflate(R.layout.layout_empty, null, false);
        mAdapter.setEmptyView(layEmpty);
    }

    public void registEmptyView(AppBaseQuickAdapter mAdapter, int resource) {
        View layEmpty = LayoutInflater.from(mContext).inflate(resource, null, false);
        mAdapter.setEmptyView(layEmpty);
    }

    NestedScrollView scrollerview;

    protected void registScrollToTopAndBottom(NestedScrollView view) {
        this.scrollerview = view;
    }

    public void goTop(View view) {
        if (scrollerview != null) {
            scrollerview.fullScroll(View.FOCUS_UP);//
        }
    }

    public void goBottom(View view) {
        if (scrollerview != null) {
            scrollerview.fullScroll(View.FOCUS_DOWN);
        }
    }

    public void registEmptyViewSimpleText(AppBaseQuickAdapter mAdapter, String text) {
        View layEmpty = LayoutInflater.from(mContext).inflate(R.layout.lay_empty_simple_text, null, false);
        TextView textView = layEmpty.findViewById(R.id.text);
        textView.setText(text);
        mAdapter.setEmptyView(layEmpty);
    }

    @Override
    public void toast(String msg) {
        if (!TextUtils.isEmpty(msg))
            XToast.toast(mContext, msg);
    }


    protected void registScrollToTop(View Button, View scrollerview) {
        Button.setOnClickListener(v -> {
            if (scrollerview != null) {
                scrollerview.scrollTo(0, 0);
            }
        });
    }


    public Context getCurrentContext() {
        return mContext;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case LoginActivity.REQUEST_CODE:
                isJumpToLogin = false;
                if (resultCode == RESULT_OK)
                    initData();
                break;
        }
    }

    public void reset() {
        initData();
    }


    @Override
    public void clearNetWork() {
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear(); // clear时网络请求会随即cancel
            mCompositeDisposable = null;
        }
    }
}
